home *** CD-ROM | disk | FTP | other *** search
- unit Filelist;
-
- interface
-
- uses Classes,SysUtils,Controls,Forms;
-
- type
- TFileList = Class(TObject)
- {Only code in the current unit can access private data fields & methods}
- private
- File_Mask: string;
- Sub_Directories: boolean;
- procedure Search_Directory ( const Directory_FileMask: string );
- procedure Found_File_Dir ( const SearchRec: TSearchRec; FileDir_List: TStringList);
- {All items in the public section can be referenced from any other unit}
- public
- Files_Found: TStringList;
- constructor Create (const Directory_FileMask: string; const Sub_Dirs: boolean);
- destructor Destroy; override;
- end; { TFileList }
-
- implementation
-
- {The next line will make all run-time I/O errors into exceptions.}
- {$I+}
-
- constructor TFileList.Create (const Directory_FileMask: string; const Sub_Dirs: boolean);
- var
- Old_Screen_Cursor: TCursor;
- begin
- {Execute the ancestor constructor}
- inherited Create;
- Old_Screen_Cursor:= Screen.Cursor;
- Screen.Cursor:= crHourGlass;
- Application.ProcessMessages;
- try
- Files_Found:= TStringList.Create;
- Files_Found.Sorted:= False;
- File_Mask:= ExtractFileName( Directory_FileMask );
- Sub_Directories:= Sub_Dirs;
- Search_Directory ( Directory_FileMask );
- finally
- Screen.Cursor:= Old_Screen_Cursor;
- end;
- end; {. Create}
-
- destructor TFileList.Destroy;
- begin
- Files_Found.Free;
- {Execute the ancestor constructor}
- inherited Destroy;
- end; {. Destroy}
-
- procedure TFileList. Search_Directory ( const Directory_FileMask: string );
- var
- SearchRec: TSearchRec;
- Directory: string;
- FilesDirsFound: TStringList;
- c1: integer;
- begin
- Directory:= ExtractFilePath (Directory_FileMask);
- FilesDirsFound:= TStringList.Create;
- FilesDirsFound.Sorted:= True;
- FilesDirsFound.Duplicates:= dupError;
- try
- {Build a list of files but not directories in current directory}
- if FindFirst (Directory_FileMask, faAnyFile - faDirectory, SearchRec) = 0 then
- begin
- Found_File_Dir ( SearchRec,FilesDirsFound);
- while ( FindNext (SearchRec) = 0 ) do
- Found_File_Dir ( SearchRec,FilesDirsFound);
- end;
- FindClose (SearchRec);
- {Now copy list of sorted files found on to public stringlist of unsorted files}
- for c1:= 0 to FilesDirsFound.Count-1 do
- Files_Found.Add( Directory + FilesDirsFound[c1] );
- {Now start checking the sub-directories}
- if Sub_Directories then
- begin
- FilesDirsFound.Clear;
- if FindFirst (Directory + '*.*', faDirectory, SearchRec) = 0 then
- begin
- Found_File_Dir ( SearchRec,FilesDirsFound);
- while ( FindNext (SearchRec) = 0 ) do
- Found_File_Dir ( SearchRec,FilesDirsFound);
- end;
- FindClose (SearchRec);
- for c1:= 0 to FilesDirsFound.Count-1 do
- Search_Directory ( Directory + FilesDirsFound[c1] + '\' + File_Mask);
- end; { if Sub_Directories }
- finally
- FilesDirsFound.Free;
- end;
- end; {. Search_Directory}
-
-
- procedure TFileList. Found_File_Dir ( const SearchRec: TSearchRec; FileDir_List: TStringList);
- begin
- if ( (SearchRec.Name <> '.') and (SearchRec.Name <> '..') ) then
- FileDir_List.Add (SearchRec.Name)
- end; {. Found_File }
-
-
- end.
-